//
//  WindowView.m
//  51易借
//
//  Created by 张睿 on 2017/9/12.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "WindowView.h"

@interface WindowView ()

@property (nonatomic, weak)UIImageView * backView;
@property (nonatomic, weak)UIImageView * warnImageView;
@property (nonatomic, weak)UILabel * explationLabel;
@property (nonatomic, weak)UIButton * cancelButton;
@property (nonatomic, weak)UIButton * sureButton;


@end

@implementation WindowView


- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    UIImageView * backView = [UIImageView new];
    
    backView.image = [UIImage imageNamed:@"认证弹框"];
    
    self.backView = backView;
    
    [self addSubview:self.backView];
    
    
    UIImageView * warnImageView = [UIImageView new];
    
    warnImageView.image = [UIImage imageNamed:@"未认证"];
    
    self.warnImageView = warnImageView;
    
    [self addSubview:self.warnImageView];
    
    UILabel * explationLabel = [UILabel new];
    
    explationLabel.text = @"您还未完善资料，请先完善！";
    explationLabel.textColor = ColorWithRGB(0x8a8f99);
    explationLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    explationLabel.numberOfLines = 0;
    
    self.explationLabel = explationLabel;
    
    [self addSubview:self.explationLabel];
    
    UIButton * cancelButton = [UIButton new];
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:ColorWithRGB(0x343434) forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(didCanecelOrSureButton:) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.cancelButton = cancelButton;
    
    [self addSubview:self.cancelButton];
    
    
    UIButton * sureButton = [UIButton new];
    
    [sureButton setTitle:@"去认证" forState:UIControlStateNormal];
    [sureButton setTitleColor:TOTAL_COLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didCanecelOrSureButton:) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.sureButton = sureButton;
    
    [self addSubview:self.sureButton];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self);
    }];
    
    [self.warnImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(34 * SCREENH6);
        make.leading.mas_equalTo(self).offset(62 * SCREENH6);
        make.top.mas_equalTo(self).offset(24 * SCREENH6);
    }];
    
    [self.explationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(28 * SCREENH6);
        make.leading.mas_equalTo(self.warnImageView.mas_trailing).offset(8 * SCREENH6);
        make.width.mas_equalTo(107 * SCREENH6);
    }];
    
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(50 * SCREENH6);
        make.width.mas_equalTo(130 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(50 * SCREENH6);
        make.width.mas_equalTo(130 * SCREENH6);
    }];
}


- (void)didCanecelOrSureButton:(UIButton *)button{
    
    if (button == self.cancelButton) {
        if([self.delegate respondsToSelector:@selector(didSure:)])
        {
            [self.delegate didSure:NO];
        }
        
        if ([self.state isEqualToString:@"home" ]) {
            
          [[NSNotificationCenter defaultCenter] postNotificationName:@"didHomeCancelButton" object:nil];
        }else{
            
          [[NSNotificationCenter defaultCenter] postNotificationName:@"didRepayCancelButton" object:nil];
        }
        
        
    }else{
        
        if([self.delegate respondsToSelector:@selector(didSure:)])
        {
            [self.delegate didSure:YES];
        }
        
        if ([self.state isEqualToString:@"home" ]) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didHomeGoFinishVC" object:nil];
        }else{
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didRepayGoFinishVC" object:nil];
        }
        
        
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
